#ifndef FREYAPUBLICREGISTER_H
#define FREYAPUBLICREGISTER_H

#include "freyabasedata.h"

#include <QFile>
#include <QFileInfo>
#include <QPair>
#include <QDir>
#include <QSharedPointer>
#include <QEventLoop>
#include <QSemaphore>
#include <QMutex>
#include <QMutexLocker>

enum ConfModType{
    eCMADD = 0,
    eCMDEL = 1,
    eCMMOD = 2
};

class FreyaBaseJson;
class FreyaBaseAction;
class FreyaPublicRegister
{
public:
    explicit FreyaPublicRegister();

    bool InsertFreyaData(const FreyaData pData);
    FreyaData FindFreyaData(const QString &dataID);
    FreyaData TakeFreyaData(const QString &dataID);

    QVariantMap GetConfigFromFile(const QString &filePath);
    bool SetConfigToFile(const QString &filePath, const QVariantMap &varmap);

    QVariant GetConfig(const QStringList &configPath);
    bool SetConfig(const QStringList &configPath, const QVariant &var);
    bool InsertConfig(const QStringList &configPath, const QVariant &var);
    bool RemoveConfig(const QStringList &configPath);

    bool RegisterObject(FreyaBaseAction *actObject, const QString &actionName);
    bool UnRegisterObject(FreyaBaseAction* actObject);
    bool RegisterCommand(FreyaBaseAction* actObject, QList<quint64> commandList);
    bool UnRegisterCommand(FreyaBaseAction* actObject);
    bool InsertQuerySymbol(const QString &dataid, QSharedPointer<QSemaphore> sem);
    QSharedPointer<QSemaphore> TakeQuerySymbol(const QString &dataid);
    bool CheckQuerySymbol(const QString &dataid);
    bool CheckObjectCommand(FreyaBaseAction* actObject, quint64 command);
    QList<FreyaBaseAction *> FindActionObject(const QString &actionName);
    QString FindActionName(FreyaBaseAction *actionObject);

    void AppendCmdLogString(const quint64 &command, const QString &LogStr);
    inline QString CmdLogString(const quint64 &command)
    {
        return m_CmdLogstringHash.value(command, "0x" + QString::number(command, 16).rightJustified(16, '0'));
    }

    QHash<FreyaBaseAction*, QString> &AllRegisterAction();

private:
    bool ConfigModifyRecursion(QVariantMap &varMap, const QStringList &configPath, const ConfModType &type, const QVariant &var = QVariant());

private:
    QHash<QString, FreyaData>                           m_FreyaDataHash;
    QPair<QString, QVariantMap>                         m_FreyaConfigPair;
    QHash<FreyaBaseAction*, QString>                    m_FreyaActionHash;
    QHash<FreyaBaseAction*, QList<quint64> >            m_FreyaCmdHash;
    QHash<QString, QSharedPointer<QSemaphore> >         m_QuerySemHash;
    QHash<quint64, QString>                             m_CmdLogstringHash;

    QMutex                                              m_DataCacheMutex;
    QMutex                                              m_DataQueryMutex;
};

#endif // FREYAPUBLICREGISTER_H
